//
//  BN_SchoolActivityViewController.m
//  BabyNow
//
//  Created by 刘泽琛 on 14-5-15.
//  Copyright (c) 2014年 iMobile. All rights reserved.
//

#import "BN_SchoolActivityViewController.h"

@interface BN_SchoolActivityViewController (){
    BN_SchoolActivities *schoolActivitiesData;
    MJRefreshHeaderView *_header;
}

@end

@implementation BN_SchoolActivityViewController

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    
    [schoolActivitiesData.helper cancelQueryActionDetail];
    [schoolActivitiesData.helper cancelQueryActionList];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[NSUserDefaults standardUserDefaults] setInteger:ACTIVITY forKey:USERDEFAULTS_KINDERSECTION];
    [[NSUserDefaults standardUserDefaults]synchronize];
    // UI+64
    if (IOS7) {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    // UI
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"活动资讯";
    
    [self schoolNavgationBar:2];
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"houtui"] style:UIBarButtonItemStylePlain target:self action:@selector(backRootVC)];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    // 列表
    self.schoolActivitiesTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, FULLSCREEN_HEIGHT) style:UITableViewStylePlain];

    [self setExtraCellLineHidden:self.schoolActivitiesTableView];
    self.schoolActivitiesTableView.backgroundColor = APP_OBJECT_GRAY_COLOR;
    self.schoolActivitiesTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_schoolActivitiesTableView setDecelerationRate:0.1];
    switch (MAIN_VERSION) {
        case 6:
            self.schoolActivitiesTableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-44);
            break;
        default:
            
            self.schoolActivitiesTableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, FULLSCREEN_HEIGHT);
            self.schoolActivitiesTableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
            self.schoolActivitiesTableView.scrollIndicatorInsets = UIEdgeInsetsMake(64, 0, 0, 0);
            break;
    }
    
    __weak typeof(self) weakSelf = self;
    schoolActivitiesData = [[BN_SchoolActivities alloc]init];
    [schoolActivitiesData toSchoolReloadList:^(BOOL isReload, BOOL isDele, NSArray *indexArr) {
        if (!isDele) {
            if (isReload) {
                [weakSelf cancelHintView];
                [weakSelf.schoolActivitiesTableView reloadData];
            }else {
                [weakSelf cancelHintView];
            }
        }else {
            [LZCHINT title:@"活动删除成功" type:LZCSuccess duration:3 places:HINT_PLACES callBack:NULL];
            [weakSelf.schoolActivitiesTableView reloadData];
        }

    }];
    self.schoolActivitiesTableView.delegate = schoolActivitiesData;
    self.schoolActivitiesTableView.dataSource = schoolActivitiesData;
    self.schoolActivitiesTableView.backgroundColor = APP_OBJECT_GRAY_COLOR;
    [self.view addSubview:self.schoolActivitiesTableView];
    
    // 下拉刷新
    MJRefreshHeaderView *header = [MJRefreshHeaderView header];
    header.scrollView = self.schoolActivitiesTableView;
    header.delegate = schoolActivitiesData;
    _header = header;
    
    // 小球提示
    [self viewLoadHintView];
    
    // 通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(openXQPage:) name:KKINDER_OPEN_ACTIVITY_XQ object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(activityModelRefresh:) name:KSENDSUCCESS_AFTER_ACTIVITY_REFRESH object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(activityDele:) name:K_SCHOOL_ACTIVITY_DELE object:nil];
}

- (void)setExtraCellLineHidden:(UITableView *)tableView
{
    UIView *view = [UIView new];
    view.backgroundColor = APP_OBJECT_GRAY_COLOR;
    [tableView setTableFooterView:view];
}

#pragma mark
#pragma mark Action


#pragma mark 通知
- (void)activityDele:(NSNotification *)notification {
    [schoolActivitiesData activityModelDataRemove:notification.object];
}

- (void)backRootVC {
    [self releaseResource];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)goCreateVC {
    BN_CreateContentViewController *createVC = [[BN_CreateContentViewController alloc]init];
    [createVC toHintMessage:^(BOOL isSuccess) {
        [LZCHINT title:@"更新成功" type:LZCSuccess duration:2 places:HINT_PLACES callBack:NULL];
    }];
    [self.navigationController pushViewController:createVC animated:YES];
}

- (void)openXQPage:(NSNotification *)notification {
    NSString *objctId = notification.object;
    BN_ContentXQViewController *kindexqVC_ = [[BN_ContentXQViewController alloc]init];
    kindexqVC_.objctId = objctId;
    [self.navigationController pushViewController:kindexqVC_ animated:YES];
}

- (void)activityModelRefresh:(NSNotification *)notification {
    [schoolActivitiesData activityRefrsh];
}

#pragma mark
#pragma mark 生命周期
- (void)releaseResource {
    [_header free];
    _header.delegate = nil;
    _header = nil;
    
    self.schoolActivitiesTableView.delegate = nil;
    self.schoolActivitiesTableView.dataSource = nil;
    self.schoolActivitiesTableView = nil;
    
    [schoolActivitiesData.helper cancelQueryActionList];
    schoolActivitiesData.helper = nil;
    schoolActivitiesData.reloadList = nil;
    schoolActivitiesData = nil;
    [self cancelHintView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)dealloc
{
    [self releaseResource];
    NSLog(@"学校活动释放...");
}
@end
